package com.zzdz.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzdz.common.entity.QueryRequest;
import com.zzdz.system.entity.Dict;
import com.zzdz.system.entity.DictItem;
import com.zzdz.system.mapper.DictItemMapper;
import com.zzdz.system.mapper.DictMapper;
import com.zzdz.system.service.IDictItemService;
import com.zzdz.system.service.IDictService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * 字典表 Service实现
 *
 * @author Zzdz
 * @date 2020-05-20 18:05:32
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private IDictItemService dictItemService;
    @Autowired
    private DictItemMapper dictItemMapper;

    @Override
    public IPage<Dict> findDicts(QueryRequest request, Dict dict) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(dict.getType()), Dict::getType, dict.getType());
        Page<Dict> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Dict> findDicts(Dict dict) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        if (dict != null) {
            queryWrapper.lambda().like(StringUtils.isNotBlank(dict.getType()), Dict::getType, dict.getType());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createDict(Dict dict) {
        dict.setCreateTime(new Date());
        this.save(dict);
    }

    @Override
    @Transactional
    public void updateDict(Dict dict) {
        dict.setUpdateTime(new Date());
        this.saveOrUpdate(dict);
    }

    @Override
    @Transactional
    public void deleteDict(Dict dict) {
        LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<>();
        // TODO 设置删除条件
        this.remove(wrapper);
    }

    @Override
    @Transactional
    public void deleteDictByIds(String ids) {

        if (StringUtils.isNotBlank(ids)) {
            // 删除dict数据
            String[] idAttr = ids.split(",");
            dictMapper.deleteBatchIds(Arrays.asList(idAttr));
            // 删除dictItem 数据
            for (String id : idAttr) {
                DictItem dictItem = new DictItem();
                dictItem.setDictId(Long.parseLong(id));
                List<DictItem> dictItemList = dictItemService.findDictItems(dictItem);
                if (dictItemList != null && dictItemList.size() > 0) {
                    StringBuffer itemIds = new StringBuffer();
                    dictItemList.forEach(item -> {
                        itemIds.append(String.valueOf(item.getId()));
                        itemIds.append(",");
                    });
                    itemIds.deleteCharAt(itemIds.length() - 1);
                    dictItemService.deleteDictItemByIds(itemIds.toString());

                }
            }
        }


    }
}
